-
Notifications
You must be signed in to change notification settings - Fork 564
Remove restriction on dereferencing pointers in const #2090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ehuss
merged 2 commits into
master
from
TC/remove-restriction-on-dereferencing-pointers-in-const
Nov 18, 2025
Merged
Remove restriction on dereferencing pointers in const #2090
ehuss
merged 2 commits into
master
from
TC/remove-restriction-on-dereferencing-pointers-in-const
Nov 18, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3dab34b to
71865f1
Compare
71865f1 to
7fb0ff8
Compare
We had said that the dereference operator could not be used with raw pointers in a constant expression. However, that restriction has been lifted. First, in Rust 1.58, we stabilized `const_raw_ptr_deref`. rust-lang/rust#89551 This allowed for dereferencing immutable raw pointers in a constant expression. Then, in Rust 1.83, we stabilized `const_mut_refs` and `const_refs_to_cell`. rust-lang/rust#129195 That allowed for: - Mentioning `&mut` types. - Creating `&mut` and `*mut` values. - Creating `&T` and `*const T` values where `T` contains interior mutability. - Dereferencing `&mut` and `*mut` values (both for reads and writes). Let's remove the stated restriction on dereferencing raw pointers in a constant expression and add examples.
To define what's allowed in a constant expression, we have a list that starts with the text: > The following expressions are constant expressions... Correspondingly, most of the items in this list are stated in terms of being a "this expression" or a "that expression". However, for deref, we had instead said "the dereference operator". Even though the `expr.deref` section is currently titled "the dereference operator", it seems more clear and consistent in the context of this list to talk about dereference expressions, so let's do that. These are expressions, after all, The grammar production is named `DereferenceExpression`. (There is one other place in the list that we talk about operators: "built-in negation, arithmetic, logical, comparison, or lazy boolean operators used on integer and floating point types, `bool`, and `char`." Perhaps we'll reword that one later. It stands out a bit less than this one did.)
7fb0ff8 to
0659e98
Compare
ehuss
approved these changes
Nov 18, 2025
Contributor
ehuss
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Nov 29, 2025
Update books ## rust-lang/book 2 commits in f78ab89d7545ac17780e6a367055cc089f4cd2ec..8c0eacd5c4acbb650497454f3a58c9e8083202a4 2025-11-18 15:36:41 UTC to 2025-11-18 15:33:41 UTC - Update ch07-02-defining-modules-to-control-scope-and-privacy.md (rust-lang/book#4570) - use AND for search terms (rust-lang/book#4573) ## rust-lang/reference 4 commits in f9f1d2a4149f02582aec2f8fcdfa5b596193b4e2..f2ac173df9906de5c03b0ee50653321ef1c4ebe8 2025-11-26 02:52:23 UTC to 2025-11-18 21:54:51 UTC - document `cfg` conditions on inline assembly templates and operands (rust-lang/reference#2063) - remove unused "link reference definitions" (rust-lang/reference#2092) - Add review process overview to review-policy.md (rust-lang/reference#2088) - Remove restriction on dereferencing pointers in const (rust-lang/reference#2090) ## rust-lang/rust-by-example 5 commits in f944161716230641605b5e3733e1c81f10047fd4..111cfae2f9c3a43f7b0ff8fa68c51cc8f930637c 2025-11-27 20:16:42 UTC to 2025-11-20 21:40:02 UTC - Use `From::from` fn pointer to convert to boxed errors (rust-lang/rust-by-example#1906) - link the _tuple_ page instead "TupleStruct" (rust-lang/rust-by-example#1909) - enum_use.md: avoid an uncommon term (rust-lang/rust-by-example#1976) - make search less surprising (rust-lang/rust-by-example#1975) - Update documentation for `any` function in iter_any.md (rust-lang/rust-by-example#1973)
rust-timer
added a commit
to rust-lang/rust
that referenced
this pull request
Nov 29, 2025
Rollup merge of #149424 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 2 commits in f78ab89d7545ac17780e6a367055cc089f4cd2ec..8c0eacd5c4acbb650497454f3a58c9e8083202a4 2025-11-18 15:36:41 UTC to 2025-11-18 15:33:41 UTC - Update ch07-02-defining-modules-to-control-scope-and-privacy.md (rust-lang/book#4570) - use AND for search terms (rust-lang/book#4573) ## rust-lang/reference 4 commits in f9f1d2a4149f02582aec2f8fcdfa5b596193b4e2..f2ac173df9906de5c03b0ee50653321ef1c4ebe8 2025-11-26 02:52:23 UTC to 2025-11-18 21:54:51 UTC - document `cfg` conditions on inline assembly templates and operands (rust-lang/reference#2063) - remove unused "link reference definitions" (rust-lang/reference#2092) - Add review process overview to review-policy.md (rust-lang/reference#2088) - Remove restriction on dereferencing pointers in const (rust-lang/reference#2090) ## rust-lang/rust-by-example 5 commits in f944161716230641605b5e3733e1c81f10047fd4..111cfae2f9c3a43f7b0ff8fa68c51cc8f930637c 2025-11-27 20:16:42 UTC to 2025-11-20 21:40:02 UTC - Use `From::from` fn pointer to convert to boxed errors (rust-lang/rust-by-example#1906) - link the _tuple_ page instead "TupleStruct" (rust-lang/rust-by-example#1909) - enum_use.md: avoid an uncommon term (rust-lang/rust-by-example#1976) - make search less surprising (rust-lang/rust-by-example#1975) - Update documentation for `any` function in iter_any.md (rust-lang/rust-by-example#1973)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We had said that the dereference operator could not be used with raw pointers in a constant expression. However, that restriction has been lifted. First, in Rust 1.58, we stabilized
const_raw_ptr_deref.rust-lang/rust#89551
This allowed for dereferencing immutable raw pointers in a constant expression. Then, in Rust 1.83, we stabilized
const_mut_refsandconst_refs_to_cell.rust-lang/rust#129195
That allowed for:
&muttypes.&mutand*mutvalues.&Tand*const Tvalues whereTcontains interior mutability.&mutand*mutvalues (both for reads and writes).Let's remove the stated restriction on dereferencing raw pointers in a constant expression and add examples.
cc @ehuss @RalfJung
I noticed this when double-checking that we didn't need to do anything for:
cc @rust-lang/fls